package com.xiaoyi.analyzer;

import com.xiaoyi.enums.ResultTypeAggEnum;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author xiaoyi
 * @version 1.0.0
 * @createTime 2023-02-19 22:23:00
 */
public class DataAnalyzerFactory {
   static Map<ResultTypeAggEnum,Class> analyzerMap = new HashMap<>();
    static {
        analyzerMap.put(ResultTypeAggEnum.PIE,PieDataAnalyzer.class);
        analyzerMap.put(ResultTypeAggEnum.LINE,LineDataAnalyzer.class);
        analyzerMap.put(ResultTypeAggEnum.BAR,BarDataAnalyzer.class);
        analyzerMap.put(ResultTypeAggEnum.PIE,NormalAggregateDataAnalyzer.class);

    }

    public static DataAnalyzer getDataAnalyzer(ResultTypeAggEnum aggEnum){
        Class aClass = analyzerMap.get(aggEnum);
        if (Objects.isNull(aClass)){
            return null;
        }
        try {
           return (DataAnalyzer) aClass.getConstructor().newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

}
